package top.lyc829.zzl_android.ui.message.system;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.NoticeSystemBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.database.dao.NoticeCommentDao;
import top.lyc829.zzl_android.logic.database.dao.NoticeSystemDao;
import top.lyc829.zzl_android.logic.database.MyDataBase;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class NoticeSystemViewModel extends AndroidViewModel {

    private MutableLiveData<List<NoticeSystemBean>> noticeSystemsLiveData;
    private NoticeSystemDao noticeSystemDao;

    public NoticeSystemViewModel(@NonNull Application application) {
        super(application);

        MyDataBase myDataBase = MyDataBase.getInstance(application);
        noticeSystemDao = myDataBase.getNoticeSystemDao();
    }


    public MutableLiveData<List<NoticeSystemBean>> getNoticeSystemsLiveData() {
        if (noticeSystemsLiveData == null) {
            noticeSystemsLiveData = new MutableLiveData<>();
        }
        return noticeSystemsLiveData;
    }

    public void loadNoticeSystems(int userId) {
        Network.loadNoticeSystem(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<List<NoticeSystemBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, NoticeSystemBean.class);
                if (resultBean.getCode().equals(200)) {

                    mergeData(resultBean.getData());
                    //getNoticeSystemsLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    private void mergeData(List<NoticeSystemBean> data) {
        if (data != null) {
            for (NoticeSystemBean system : data) {
                if (noticeSystemDao.findItemById(system.getId()) == null) {
                    noticeSystemDao.insertNoticeSystems(system);
                }
            }
        }

        getNoticeSystemsLiveData().postValue(noticeSystemDao.getAllNoticeSystems());
    }

    /**
     * 在数据库中删除通知
     * @param systemBean 系统通知
     */
    public void deleteSystemData(NoticeSystemBean systemBean) {
        if (systemBean != null) {
            new Thread(() -> {
                noticeSystemDao.deleteNoticeSystems(systemBean);
            }).start();
        }

    }

}
